import styles from './index.module.scss'
import { NavBar, TextArea } from 'antd-mobile'
import { TextAreaRef } from 'antd-mobile/es/components/text-area'
import { useEffect, useRef, useState } from 'react'
type Props = {
  // 评论的作者的名字
  name?: string
  closePopup?: () => void
  publish?: (val: string) => void
}

export default function CommentInput({ name, closePopup, publish }: Props) {
  const textRef = useRef<TextAreaRef>(null)

  const [content, setContent] = useState('')

  useEffect(() => {
    textRef.current!.focus()
  }, [])

  return (
    <div className={styles.root}>
      <NavBar
        right={
          <span
            className="publish"
            onClick={() => {
              publish?.(content)
              // setContent('')
            }}
          >
            发表
          </span>
        }
        onBack={() => {
          closePopup?.()
        }}
      >
        {name ? '回复评论' : '评论文章'}
      </NavBar>
      <div className="input-area">
        {/* 回复别人的评论时显示：@某某 */}
        {name && <div className="at">@{name}:</div>}

        {/* 评论内容输入框 */}
        <TextArea
          value={content}
          onChange={(val) => {
            setContent(val)
          }}
          placeholder="说点什么~"
          rows={10}
          ref={textRef}
        />
      </div>
    </div>
  )
}
